package com.kyzh.core.adapters.v3

import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.GroupChatItem
import com.gushenge.core.beans.Sort
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import org.jetbrains.anko.toast

class HomeGroupChatBottomAdapter(layout:Int, titles:ArrayList<Sort>):BaseQuickAdapter<Sort,BaseViewHolder>(layout,titles) {

    override fun convert(holder: BaseViewHolder, item: Sort) {
        val root = holder.getView<SmartRefreshLayout>(R.id.root)
        val recyclerView = holder.getView<RecyclerView>(R.id.recyclerView)
        val bean = arrayListOf<GroupChatItem>()
        recyclerView.layoutManager = object : LinearLayoutManager(context) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        val adapter = HomeGroupChatListAdapter(R.layout.item_groupchat, bean)
        recyclerView.adapter = adapter
        adapter.setEmptyView(R.layout.empty)
        var p1 = 1
        var max1 = 1
        root.setEnableAutoLoadMore(false)
        root.setEnableRefresh(true)
        GameRequest.groupChat(item.id,1){p, max, data, tuijian ->
            bean.clear()
            adapter.addData(data)
            p1 = p
            max1 = max
            root.finishRefresh()
            Log.e("convert: ",data.toString() )
        }
        root.setOnLoadMoreListener {
            if (p1 > max1) {
                context.toast(context.getString(R.string.noHaveMore))
                root.finishLoadMore()
            } else {
                GameRequest.groupChat(item.id,p1){p, max, data, tuijian ->
                    adapter.addData(data)
                    p1 = p
                    max1 = max
                    root.finishLoadMore()
                }
            }
        }
        root.setOnRefreshListener {
            GameRequest.groupChat(item.id,1){p, max, data, tuijian ->
                bean.clear()
                adapter.addData(data)
                p1 = p
                max1 = max
                root.finishRefresh()
            }

        }
    }

}